<?php
class Dashboard extends Controller {

	function Dashboard()
	{
		parent::Controller();	
		$this->users->check();
		$this->load->helper('file');
		
	}
	function index()
	{
		/*if(read_file('system/application/controllers/install.php')) {
		if ( ! unlink('system/application/controllers/install.php')) {
			$data['err'] = '<div class="error">You need to manually delete <strong>install.php</strong> located at <strong>system/application/controllers/install.php</strong></div>';
		}*/
		$template['tabs'] = array(
					anchor('designs', 'Upload a design'),
					anchor('discussions', 'Start a discussion'),
					anchor('todos', 'Add a todo'),
					anchor('files', 'Share a file')
					);
		$template['title'] = 'Home';
		// Get the latest design
		$query = $this->db->query('SELECT * FROM designs LEFT JOIN threads ON threads.id = designs.threadid ORDER BY designs.designid DESC LIMIT 1');
		foreach($query->result() as $row) {

		// Get info for the latest design
		$data['lastimage'] = anchor('designs/thread/' . $row->threadid, img('designs/med_' . $row->image));
		$data['thread'] = $row->name;
		$data['tid'] = $row->threadid;
		}
		//Get recent designs, offset by 1 to not include the latest, which is already accounted for
		$data['recent'] = $this->db->query('SELECT * FROM designs ORDER BY designid DESC LIMIT 1, 2');
		
		//Get the todos
		$data['todos'] = $this->db->query("SELECT *, FROM_UNIXTIME(todos.bydate, '%M %e') AS thedate  FROM todos LEFT JOIN users ON users.id = todos.assignto WHERE todos.completed = '0' ORDER BY todos.bydate ASC LIMIT 5");
		
		//Get the discussions 
		$data['discussions'] = $this->db->query('SELECT *, COUNT(posts.postid) AS count, discussions.id AS did FROM discussions LEFT JOIN users ON discussions.tuser = users.id LEFT JOIN posts ON posts.discussion = discussions.id GROUP BY discussions.title ORDER BY discussions.id DESC LIMIT 5');
		
		// Get the files
		$data['files'] = get_filenames('files');
			
		$template['content'] = $this->load->view('dash', $data, TRUE);
		$this->load->view('template', $template);
	}
}
?>